<?php

/**
 * @author ismael gonzalez
 * @copyright 2010
 * manejador de Album de fotos de las Galerias
 */
class Album{
	public function addAlbum($titulo, $descripcion, $fechaPub, $tipo){
		
		$arrFecha = explode("-", $fechaPub);
		$fechaPub = $arrFecha[2]."-".$arrFecha[1]."-".$arrFecha[0];
		
		$SQL = "INSERT INTO album (titulo, descripcion, fechaPublicacion, fechaCreado, "
			."creadoPor, tipo) VALUES ('".$titulo."', '".$descripcion."', '"
		 	.$fechaPub."', '".date('Y-m-d H:i:s')."', ".$_SESSION['AdminID'].", '"
			 .$tipo."')";
		$Q = mysql_query($SQL) or die(mysql_error());
	}
	
	public function getUltimosAlbums(){
		$SQL = "SELECT albumid, titulo FROM album WHERE fechaPublicacion <= '"
				.date('Y-m-d')."' ORDER BY fechaPublicacion DESC LIMIT 5";
		$Q = mysql_query($SQL) or die(mysql_error());
		
		$opt = "<option value='0'>--Albums--</option>";
		while($row = mysql_fetch_array($Q)){
			$opt .= "<option value='".$row["albumid"]."' ";
			
			if($_POST['album']== $row["albumid"]){
				$opt .= " selected ";
			}
			$opt .= ">".$row["titulo"]."</option>";
		}
		
		return $opt;
	}
	
	public function getTopQueFreson(){
		$SQL = "SELECT a.titulo, a.descripcion, f.fotoid, f.foto, a.albumid FROM album a "
				."LEFT JOIN foto f ON a.albumid = f.albumid "
				."WHERE tipo = 'queFreson' AND fechaPublicacion <= curDate() "
				."AND a.status='A' "
				."ORDER BY albumid DESC LIMIT 1";
		$Q = mysql_query($SQL)or die(mysql_error());
		$row = mysql_fetch_array($Q);
		
		return $row;
	}
	
	public function getTopFresonPolitico(){
		$SQL = "SELECT a.titulo, a.descripcion, f.fotoid, f.foto, a.albumid FROM album a "
				."LEFT JOIN foto f ON a.albumid = f.albumid "
				."WHERE tipo = 'fresonPolitico' AND fechaPublicacion <= curDate() "
				."AND a.status='A' "
				."ORDER BY albumid DESC LIMIT 1";
		$Q = mysql_query($SQL)or die(mysql_error());
		$row = mysql_fetch_array($Q);
		
		return $row;
	}
	
	public function getTopQueGuau(){
		$SQL = "SELECT a.titulo, a.descripcion, f.fotoid, f.foto, a.albumid FROM album a "
				."LEFT JOIN foto f ON a.albumid = f.albumid "
				."WHERE tipo = 'queGuau' AND fechaPublicacion <= curDate() "
				."AND a.status = 'A' "
				."ORDER BY albumid DESC LIMIT 1";

		$Q = mysql_query($SQL)or die(mysql_error());
		$row = mysql_fetch_array($Q);
		
		return $row;
	}
	
	public function getFotosFromAlbum($albumID){
		$SQL = "SELECT f.fotoid, f.foto "
			."FROM album a LEFT JOIN foto f ON a.albumid = f.albumid "
			."WHERE f.status='A' AND a.albumid = ".$albumID;

		$Q = mysql_query($SQL)or die(mysql_error());
		$i = 0;
		$arrResults = Array();
		while($row = mysql_fetch_array($Q)){
			$arrResults[$i]["foto"]= $row["foto"];			
			$arrResults[$i]["fotoid"]= $row["fotoid"];
			$i++;
		}
		
		return $arrResults;
	}
	
	public function getAlbumName($albumID){
		$SQL = "SELECT titulo FROM album WHERE albumid = ".$albumID;
		$Q = mysql_query($SQL)or die(mysql_error());
		$row = mysql_fetch_array($Q);
		
		return $row;
	}

	public function getTop10() {
		$SQL = "SELECT albumid, titulo FROM album WHERE status='A' ORDER BY fechaPublicacion DESC, albumid DESC LIMIT 10";

		$q = mysql_query($SQL) or die(mysql_error());

		$arrAlbum = array();
		$i = 0;

		while ($row = mysql_fetch_array($q)) {
			$arrAlbum[$i]['albumid'] = $row['albumid'];
			$arrAlbum[$i]['titulo'] = $row['titulo'];
			$i++;
		}

		return $arrAlbum;
	}
}

?>
